﻿#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct Person
{
	string name;
	int age;
	int high;
	int save;
};
int cmp_int(const void* e1, const void* e2)
{
	return *((int*)e1) - *((int*)e2);
}
int cmp_float(const void* e1, const void* e2)
{
	float f1 = *((float*)e1);
	float f2 = *((float*)e2);
	if (f1 < f2)
	{
		return -1;
	}
	else if (f1 > f2)
	{
		return 1;
	}
	else return 0;
}
int cmp_struct_Person(const void* e1, const void* e2)
{
	int p1 = ((Person*)e1)->age;
	int p2 = ((Person*)e2)->age;
	return p1 - p2;
}

void bubble_sort(void* arr, int sz, int width, int(*cmp)(const void*, const void*))
{
	for(int i=0;i<sz-1;i++)
		for (int j = i + 1; j < sz; j++)
		{
			char* e1 = (char*)arr + i * width;
			char* e2 = (char*)arr + j * width;
			if (cmp(e1, e2) > 0)
			{
				for (int k = 0; k < width; k++)
				{
					char tem = *((char*)e1 + k);
					*((char*)e1 + k) = *((char*)e2 + k);
					*((char*)e2 + k) = tem;
				}
			}


		}
}
int main()
{
	Person person[5] = { {"阿卡丽",18,170,2250},{"盖伦",25,185,3500},{"费德提克",999,75,1500},{"妖姬",1500,165,3000},{"亚索",35,175,2500}};
	
	bubble_sort(person, sizeof(person) / sizeof(person[0]), sizeof(Person), cmp_struct_Person);
	
	for (int i = 0; i < sizeof(person) / sizeof(person[0]);i++)cout<<person[i].name<<"的年龄是："<<person[i].age<<"岁"<<endl;

	int arr[] = { 5,7,1,6,4,9,3,4,2,3,4,8,9,4,2,3,6,4,1,2,3,6,4,8,9,7,2,6,1,10,0,15,20,18,17,18,5,6,4,3 };

	bubble_sort(arr, sizeof(arr) / sizeof(arr[0]), sizeof(int), cmp_int);

	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)cout << arr[i] << " ";

	cout << endl;

	float f_arr[] = { 1.25,3.1415,2.717,2.55,1.345,0.996,0.007,1.75,1.65,1.125,3.125,1.40096,1.685,1.444,1.425 };

	bubble_sort(f_arr, sizeof(f_arr) / sizeof(f_arr[0]), sizeof(float), cmp_float);

	for(int i=0;i< sizeof(f_arr) / sizeof(f_arr[0]);i++)cout<< f_arr[i] << " ";

	cout << endl;
	return 0;
}